﻿
using UnityEngine;
using System.IO;
using UnityEngine.UI;
using UnityEngine.U2D;

public class UIAssetBundle : MonoBehaviour
{
    private string path;

    public GameObject imageGo;
    void Start()
    {
        path = Application.persistentDataPath;
        Debug.Log(Application.streamingAssetsPath);
        //GameObject obj = (GameObject)LoadRes(LoadAssetBundle("OutPut/role.dat"), "roleImage");
        //if (obj != null) ;
        //把sprite打在一个aseetbundle里面

        for (int i = 1; i <= 10; i++)
        {
            GameObject obj = Instantiate(imageGo);
            obj.transform.SetParent(transform);
            obj.transform.localPosition = new Vector3(Random.Range(-142, 142), Random.Range(-310, 310), 0);
            obj.transform.localScale = Vector3.one;
            obj.GetComponent<Image>().sprite = LoadSpriteFroomAtlas(i.ToString());
        }
        GameObject obj1 = Instantiate(imageGo);
        obj1.transform.SetParent(transform);
        obj1.transform.localPosition = new Vector3(Random.Range(-142, 142), Random.Range(-310, 310), 0);
        obj1.transform.localScale = Vector3.one;
        obj1.GetComponent<Image>().sprite = LoadSpriteFroomAtlas("beijing");

        //FenSan(obj);
        //All(obj);

    }

    private Sprite LoadSpriteFroomAtlas(string spriteName)
    {

    //    /*

    //      assetBundle 加载：

    //    var atlas= assetbundle.loadAsset<SpriteAtlas>(atlasName+varitant);
    //    */

    //    var atlas = Resources.Load<SpriteAtlas>("main");
    //    Sprite s = atlas.GetSprite(spriteName);
        return null;

    }


    private void All(GameObject obj)
    {
        Sprite[] sprit = LoadSprite(LoadAssetBundle("OutPut1/roleimage.dat"));
        for (int i = 0; i < sprit.Length - 5; i++)
        {
            GameObject role = Instantiate(obj);
            role.transform.SetParent(transform);
            role.transform.localPosition = new Vector3(i * 20, i * 10, 0);
            role.transform.localScale = Vector3.one;
            role.GetComponent<Image>().sprite = sprit[i];
        }

    }
    private void FenSan(GameObject obj)
    {
        //分开打

        for (int i = 1; i < 6; i++)
        {
            Sprite s = LoadSprite(LoadAssetBundle("OutPut/" + i.ToString() + ".dat"), i.ToString());
            GameObject role = Instantiate(obj);
            role.transform.SetParent(transform);
            role.transform.localPosition = new Vector3(i * 20, i * 10, 0);
            role.transform.localScale = Vector3.one;
            role.GetComponent<Image>().sprite = s;
        }
    }
    public Object LoadRes(AssetBundle bundle, string resName, bool isUnload = true)
    {


        Object obj = bundle.LoadAsset(resName);
        if (isUnload)
            Invoke("Unload", 1f);
        return obj;
    }
    public Sprite LoadSprite(AssetBundle bundle, string resName, bool isUnload = true)
    {


        Sprite obj = bundle.LoadAsset<Sprite>(resName);
        if (isUnload)
            //bundle.Unload(false);
            Invoke("Unload", 1f);
        return obj;
    }
    public Sprite[] LoadSprite(AssetBundle bundle, bool isUnload = true)
    {


        Sprite[] obj = bundle.LoadAllAssets<Sprite>();
        if (isUnload)
            Invoke("Unload", 1f);
        return obj;
    }
    public AssetBundle LoadAssetBundle(string bundlePath)
    {
        string loadpath = Path.Combine(path, bundlePath);
        Debug.Log(loadpath);
        byte[] bytes = File.ReadAllBytes(loadpath);
        AssetBundle bundle = AssetBundle.LoadFromMemory(bytes);
        if (bundle != null)
            return bundle;

        return null;
    }
    private void Unload(AssetBundle bundle)
    {
        if (bundle != null)
        {
            bundle.Unload(false);
        }
    }
    void OnGUI()
    {

#if UNITY_EDITOR
        GUILayout.Space(50);
        GUILayout.Label("Total DrawCall: " + UnityEditor.UnityStats.drawCalls, GUILayout.Width(500));
        GUILayout.Label("Batch: " + UnityEditor.UnityStats.batches, GUILayout.Width(500));
        GUILayout.Label("Static Batch DC: " + UnityEditor.UnityStats.staticBatchedDrawCalls, GUILayout.Width(500));
        GUILayout.Label("Static Batch: " + UnityEditor.UnityStats.staticBatches, GUILayout.Width(500));
        GUILayout.Label("DynamicBatch DC: " + UnityEditor.UnityStats.dynamicBatchedDrawCalls, GUILayout.Width(500));
        GUILayout.Label("DynamicBatch: " + UnityEditor.UnityStats.dynamicBatches, GUILayout.Width(500));
        GUILayout.Label("Tri: " + UnityEditor.UnityStats.triangles, GUILayout.Width(500));
        GUILayout.Label("Ver: " + UnityEditor.UnityStats.vertices, GUILayout.Width(500));
#endif



    }
}
